/* global Vue */
import html from './template.html'
import styles from '../../../css/index.scss'
import { decorate } from 'common/axio-decorate'
import Login from 'components/login'
import { setInitialStates } from 'store/state'
import { getStore } from 'store'
import { getUrlParams } from 'utils/qs'

// import { USER_INFO } from 'store/mutation-types'
import { quickLogin } from './models'
// import { setUserInfoToCache } from 'components/login/models'

// const envCfg = require('env.cfg')

const { appPrefix } = styles
const state = setInitialStates({ appPrefix })
const store = getStore(state)

const LoginPage = {
  template: html,
  components: {
    Login,
  },
  data() {
    return {
      // logoSrc: envCfg.publicPath + 'assets/images/logo.png'
    }
  },
  methods: {
    async $quickLogin(params) {
      try {
        const { data: token } = await quickLogin(params)
        this.isSubmiting = false
        this.$refs.login.$getAuthInfo({ data: { token } })
      } catch (err) {
        console.error(err)
        this.$message.error(err.message || '登录失败，请稍后再试')
        this.isSubmiting = false
      }
    }
  },
  mounted() {
    const customerId = getUrlParams('customerId')
    if (customerId) {
      this.$quickLogin({ id: customerId })
    }
  }
};

function bootStrap() {
  decorate()

  new Vue({
    store,
    render: (h) => {
      global.store = store
      return h(LoginPage)
    }
  }).$mount('#app')
}
bootStrap()
